package com.mt.skyhoist.util.view;

import com.mt.skyhoist.R;
import com.mt.skyhoist.util.DarkModel;
import com.mt.skyhoist.util.bean.DeviceModel;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class DeviceView extends LinearLayout {

	private Context context;

	public DeviceView(Context context) {
		super(context);
		this.context = context;

		initView();
	}

	// 初始化控件
	private View layout;
	private TextView devicename_txt;
	private ImageView rssi_imagview;

	private void initView() {
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		inflater.inflate(R.layout.fmt_device, this);

		layout = findViewById(R.id.layout);
		devicename_txt = findViewById(R.id.devicename_txt);
		rssi_imagview = findViewById(R.id.rssi_imagview);
	}

	private boolean isLinked = false;
	public void setStyle(boolean isLinked){
		this.isLinked = isLinked;
	}

	// 设置数据，更新UI
	public void setDatas(DeviceModel device) {
		// 更新蓝牙基本参数
		devicename_txt.setText(device.getName());
		
		if(device.isSelectAble()){
			layout.setBackgroundResource(R.color.colorPrimary);
		}else{
			layout.setBackgroundResource(R.color.nocolor);
		}
		
		if(device.getRssi() == 0){   // 如果是0，则不要进行显示
			rssi_imagview.setImageBitmap(null);
		}

		if (isLinked){
			if(device.isSelectAble()){
				devicename_txt.setTextColor(Color.WHITE);
				if(device.getRssi() == 0){
					rssi_imagview.setImageBitmap(null);
				} else {
					rssi_imagview.setImageResource(R.drawable.wifi_white);
				}
			} else {
				devicename_txt.setTextColor(getResources().getColor(R.color.colorPrimary));
				if(device.getRssi() == 0){
					rssi_imagview.setImageBitmap(null);
				} else {
					rssi_imagview.setImageResource(R.drawable.wifi_orange);
				}
			}
		} else {
			if(device.isSelectAble()){
				devicename_txt.setTextColor(getResources().getColor(R.color.txt_color_free_device_select));
				if(device.getRssi() == 0){
					rssi_imagview.setImageBitmap(null);
				} else {
					rssi_imagview.setImageResource(R.drawable.wifi_white);
				}
			} else {
				devicename_txt.setTextColor(getResources().getColor(R.color.txt_color_free_device_unselect));
				if(device.getRssi() == 0){
					rssi_imagview.setImageBitmap(null);
				} else {
					if(DarkModel.isDarkModel(context)) {
						rssi_imagview.setImageResource(R.drawable.wifi_white);
					} else {
						rssi_imagview.setImageResource(R.drawable.wifi_black);
					}
				}
			}
		}
	}
}
